home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb-4.5 / sun4.md / gdb / utils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-24  |  32.3 KB  |  1,380 lines

  1. /* General utility routines for GDB, the GNU debugger.
  2.    Copyright 1986, 1989, 1990, 1991, 1992 Free Software Foundation, Inc.
  3.  
  4. This file is part of GDB.
  5.  
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include "defs.h"
  21.  
  22. #include <sys/ioctl.h>
  23. #include <sys/param.h>
  24. #include <pwd.h>
  25. #include <varargs.h>
  26. #include <ctype.h>
  27. #include <string.h>
  28.  
  29. #include "signals.h"
  30. #include "gdbcmd.h"
  31. #include "terminal.h"
  32. #include "bfd.h"
  33. #include "target.h"
  34.  
  35. /* Prototypes for local functions */
  36.  
  37. #if !defined (NO_MALLOC_CHECK)
  38.  
  39. static void
  40. malloc_botch PARAMS ((void));
  41.  
  42. #endif /* NO_MALLOC_CHECK  */
  43.  
  44. static void
  45. fatal_dump_core ();    /* Can't prototype with <varargs.h> usage... */
  46.  
  47. static void
  48. prompt_for_continue PARAMS ((void));
  49.  
  50. static void 
  51. set_width_command PARAMS ((char *, int, struct cmd_list_element *));
  52.  
  53. static void
  54. vfprintf_filtered PARAMS ((FILE *, char *, va_list));
  55.  
  56. /* If this definition isn't overridden by the header files, assume
  57.    that isatty and fileno exist on this system.  */
  58. #ifndef ISATTY
  59. #define ISATTY(FP)    (isatty (fileno (FP)))
  60. #endif
  61.  
  62. /* Chain of cleanup actions established with make_cleanup,
  63.    to be executed if an error happens.  */
  64.  
  65. static struct cleanup *cleanup_chain;
  66.  
  67. /* Nonzero means a quit has been requested.  */
  68.  
  69. int quit_flag;
  70.  
  71. /* Nonzero means quit immediately if Control-C is typed now,
  72.    rather than waiting until QUIT is executed.  */
  73.  
  74. int immediate_quit;
  75.  
  76. /* Nonzero means that encoded C++ names should be printed out in their
  77.    C++ form rather than raw.  */
  78.  
  79. int demangle = 1;
  80.  
  81. /* Nonzero means that encoded C++ names should be printed out in their
  82.    C++ form even in assembler language displays.  If this is set, but
  83.    DEMANGLE is zero, names are printed raw, i.e. DEMANGLE controls.  */
  84.  
  85. int asm_demangle = 0;
  86.  
  87. /* Nonzero means that strings with character values >0x7F should be printed
  88.    as octal escapes.  Zero means just print the value (e.g. it's an
  89.    international character, and the terminal or window can cope.)  */
  90.  
  91. int sevenbit_strings = 0;
  92.  
  93. #ifdef sprite
  94. /*
  95.  * Cached value for ISATTY((stdout)). This value is cached on Sprite
  96.  * because the isatty() ioctl requires a pdev exchange with the tty driver
  97.  * taking at least 2 ms.  This makes output very slow on gdb.
  98.  */
  99. int isatty_stdout;
  100. #endif
  101.  
  102. /* String to be printed before error messages, if any.  */
  103.  
  104. char *error_pre_print;
  105. char *warning_pre_print = "\nwarning: ";
  106.  
  107. /* Add a new cleanup to the cleanup_chain,
  108.    and return the previous chain pointer
  109.    to be passed later to do_cleanups or discard_cleanups.
  110.    Args are FUNCTION to clean up with, and ARG to pass to it.  */
  111.  
  112. struct cleanup *
  113. make_cleanup (function, arg)
  114.      void (*function) PARAMS ((PTR));
  115.      PTR arg;
  116. {
  117.   register struct cleanup *new
  118.     = (struct cleanup *) xmalloc (sizeof (struct cleanup));
  119.   register struct cleanup *old_chain = cleanup_chain;
  120.  
  121.   new->next = cleanup_chain;
  122.   new->function = function;
  123.   new->arg = arg;
  124.   cleanup_chain = new;
  125.  
  126.   return old_chain;
  127. }
  128.  
  129. /* Discard cleanups and do the actions they describe
  130.    until we get back to the point OLD_CHAIN in the cleanup_chain.  */
  131.  
  132. void
  133. do_cleanups (old_chain)
  134.      register struct cleanup *old_chain;
  135. {
  136.   register struct cleanup *ptr;
  137.   while ((ptr = cleanup_chain) != old_chain)
  138.     {
  139.       cleanup_chain = ptr->next;    /* Do this first incase recursion */
  140.       (*ptr->function) (ptr->arg);
  141.       free (ptr);
  142.     }
  143. }
  144.  
  145. /* Discard cleanups, not doing the actions they describe,
  146.    until we get back to the point OLD_CHAIN in the cleanup_chain.  */
  147.  
  148. void
  149. discard_cleanups (old_chain)
  150.      register struct cleanup *old_chain;
  151. {
  152.   register struct cleanup *ptr;
  153.   while ((ptr = cleanup_chain) != old_chain)
  154.     {
  155.       cleanup_chain = ptr->next;
  156.       free ((PTR)ptr);
  157.     }
  158. }
  159.  
  160. /* Set the cleanup_chain to 0, and return the old cleanup chain.  */
  161. struct cleanup *
  162. save_cleanups ()
  163. {
  164.   struct cleanup *old_chain = cleanup_chain;
  165.  
  166.   cleanup_chain = 0;
  167.   return old_chain;
  168. }
  169.  
  170. /* Restore the cleanup chain from a previously saved chain.  */
  171. void
  172. restore_cleanups (chain)
  173.      struct cleanup *chain;
  174. {
  175.   cleanup_chain = chain;
  176. }
  177.  
  178. /* This function is useful for cleanups.
  179.    Do
  180.  
  181.      foo = xmalloc (...);
  182.      old_chain = make_cleanup (free_current_contents, &foo);
  183.  
  184.    to arrange to free the object thus allocated.  */
  185.  
  186. void
  187. free_current_contents (location)
  188.      char **location;
  189. {
  190.   free (*location);
  191. }
  192.  
  193. /* Provide a known function that does nothing, to use as a base for
  194.    for a possibly long chain of cleanups.  This is useful where we
  195.    use the cleanup chain for handling normal cleanups as well as dealing
  196.    with cleanups that need to be done as a result of a call to error().
  197.    In such cases, we may not be certain where the first cleanup is, unless
  198.    we have a do-nothing one to always use as the base. */
  199.  
  200. /* ARGSUSED */
  201. void
  202. null_cleanup (arg)
  203.     char **arg;
  204. {
  205. }
  206.  
  207.  
  208. /* Provide a hook for modules wishing to print their own warning messages
  209.    to set up the terminal state in a compatible way, without them having
  210.    to import all the target_<...> macros. */
  211.  
  212. void
  213. warning_setup ()
  214. {
  215.   target_terminal_ours ();
  216.   wrap_here("");            /* Force out any buffered output */
  217.   fflush (stdout);
  218. }
  219.  
  220. /* Print a warning message.
  221.    The first argument STRING is the warning message, used as a fprintf string,
  222.    and the remaining args are passed as arguments to it.
  223.    The primary difference between warnings and errors is that a warning
  224.    does not force the return to command level. */
  225.  
  226. /* VARARGS */
  227. void
  228. warning (va_alist)
  229.      va_dcl
  230. {
  231.   va_list args;
  232.   char *string;
  233.  
  234.   va_start (args);
  235.   target_terminal_ours ();
  236.   wrap_here("");            /* Force out any buffered output */
  237.   fflush (stdout);
  238.   if (warning_pre_print)
  239.     fprintf (stderr, warning_pre_print);
  240.   string = va_arg (args, char *);
  241.   vfprintf (stderr, string, args);
  242.   fprintf (stderr, "\n");
  243.   va_end (args);
  244. }
  245.  
  246. /* Print an error message and return to command level.
  247.    The first argument STRING is the error message, used as a fprintf string,
  248.    and the remaining args are passed as arguments to it.  */
  249.  
  250. /* VARARGS */
  251. NORETURN void
  252. error (va_alist)
  253.      va_dcl
  254. {
  255.   va_list args;
  256.   char *string;
  257.  
  258.   va_start (args);
  259.   target_terminal_ours ();
  260.   wrap_here("");            /* Force out any buffered output */
  261.   fflush (stdout);
  262.   if (error_pre_print)
  263.     fprintf (stderr, error_pre_print);
  264.   string = va_arg (args, char *);
  265.   vfprintf (stderr, string, args);
  266.   fprintf (stderr, "\n");
  267.   va_end (args);
  268.   return_to_top_level ();
  269. }
  270.  
  271. /* Print an error message and exit reporting failure.
  272.    This is for a error that we cannot continue from.
  273.    The arguments are printed a la printf.
  274.  
  275.    This function cannot be declared volatile (NORETURN) in an
  276.    ANSI environment because exit() is not declared volatile. */
  277.  
  278. /* VARARGS */
  279. NORETURN void
  280. fatal (va_alist)
  281.      va_dcl
  282. {
  283.   va_list args;
  284.   char *string;
  285.  
  286.   va_start (args);
  287.   string = va_arg (args, char *);
  288.   fprintf (stderr, "\ngdb: ");
  289.   vfprintf (stderr, string, args);
  290.   fprintf (stderr, "\n");
  291.   va_end (args);
  292.   exit (1);
  293. }
  294.  
  295. /* Print an error message and exit, dumping core.
  296.    The arguments are printed a la printf ().  */
  297.  
  298. /* VARARGS */
  299. static void
  300. fatal_dump_core (va_alist)
  301.      va_dcl
  302. {
  303.   va_list args;
  304.   char *string;
  305.  
  306.   va_start (args);
  307.   string = va_arg (args, char *);
  308.   /* "internal error" is always correct, since GDB should never dump
  309.      core, no matter what the input.  */
  310.   fprintf (stderr, "\ngdb internal error: ");
  311.   vfprintf (stderr, string, args);
  312.   fprintf (stderr, "\n");
  313.   va_end (args);
  314.  
  315.   signal (SIGQUIT, SIG_DFL);
  316.   kill (getpid (), SIGQUIT);
  317.   /* We should never get here, but just in case...  */
  318.   exit (1);
  319. }
  320.  
  321. /* Print the system error message for errno, and also mention STRING
  322.    as the file name for which the error was encountered.
  323.    Then return to command level.  */
  324.  
  325. void
  326. perror_with_name (string)
  327.      char *string;
  328. {
  329.   extern int sys_nerr;
  330.   extern char *sys_errlist[];
  331.   char *err;
  332.   char *combined;
  333.  
  334.   if (errno < sys_nerr)
  335.     err = sys_errlist[errno];
  336.   else
  337.     err = "unknown error";
  338.  
  339.   combined = (char *) alloca (strlen (err) + strlen (string) + 3);
  340.   strcpy (combined, string);
  341.   strcat (combined, ": ");
  342.   strcat (combined, err);
  343.  
  344.   /* I understand setting these is a matter of taste.  Still, some people
  345.      may clear errno but not know about bfd_error.  Doing this here is not
  346.      unreasonable. */
  347.   bfd_error = no_error;
  348.   errno = 0;
  349.  
  350.   error ("%s.", combined);
  351. }
  352.  
  353. /* Print the system error message for ERRCODE, and also mention STRING
  354.    as the file name for which the error was encountered.  */
  355.  
  356. void
  357. print_sys_errmsg (string, errcode)
  358.      char *string;
  359.      int errcode;
  360. {
  361.   extern int sys_nerr;
  362.   extern char *sys_errlist[];
  363.   char *err;
  364.   char *combined;
  365.  
  366.   if (errcode < sys_nerr)
  367.     err = sys_errlist[errcode];
  368.   else
  369.     err = "unknown error";
  370.  
  371.   combined = (char *) alloca (strlen (err) + strlen (string) + 3);
  372.   strcpy (combined, string);
  373.   strcat (combined, ": ");
  374.   strcat (combined, err);
  375.  
  376.   printf ("%s.\n", combined);
  377. }
  378.  
  379. /* Control C eventually causes this to be called, at a convenient time.  */
  380.  
  381. void
  382. quit ()
  383. {
  384.   target_terminal_ours ();
  385.   wrap_here ((char *)0);        /* Force out any pending output */
  386. #ifdef HAVE_TERMIO
  387.   ioctl (fileno (stdout), TCFLSH, 1);
  388. #else /* not HAVE_TERMIO */
  389.   ioctl (fileno (stdout), TIOCFLUSH, 0);
  390. #endif /* not HAVE_TERMIO */
  391. #ifdef TIOCGPGRP
  392.   error ("Quit");
  393. #else
  394.   error ("Quit (expect signal %d when inferior is resumed)", SIGINT);
  395. #endif /* TIOCGPGRP */
  396. }
  397.  
  398. /* Control C comes here */
  399.  
  400. void
  401. request_quit (signo)
  402.      int signo;
  403. {
  404.   quit_flag = 1;
  405.  
  406. #ifdef USG
  407.   /* Restore the signal handler.  */
  408.   signal (signo, request_quit);
  409. #endif
  410.  
  411.   if (immediate_quit)
  412.     quit ();
  413. }
  414.  
  415.  
  416. /* Memory management stuff (malloc friends).  */
  417.  
  418. #if defined (NO_MMALLOC)
  419.  
  420. PTR
  421. mmalloc (md, size)
  422.      PTR md;
  423.      long size;
  424. {
  425.   return (malloc (size));
  426. }
  427.  
  428. PTR
  429. mrealloc (md, ptr, size)
  430.      PTR md;
  431.      PTR ptr;
  432.      long size;
  433. {
  434.   if (ptr == 0)        /* Guard against old realloc's */
  435.     return malloc (size);
  436.   else
  437.     return realloc (ptr, size);
  438. }
  439.  
  440. void
  441. mfree (md, ptr)
  442.      PTR md;
  443.      PTR ptr;
  444. {
  445.   free (ptr);
  446. }
  447.  
  448. #endif    /* NO_MMALLOC */
  449.  
  450. #if defined (NO_MMALLOC) || defined (NO_MMALLOC_CHECK)
  451.  
  452. void
  453. init_malloc (md)
  454.      PTR md;
  455. {
  456. }
  457.  
  458. #else /* have mmalloc and want corruption checking  */
  459.  
  460. static void
  461. malloc_botch ()
  462. {
  463.   fatal_dump_core ("Memory corruption");
  464. }
  465.  
  466. /* Attempt to install hooks in mmalloc/mrealloc/mfree for the heap specified
  467.    by MD, to detect memory corruption.  Note that MD may be NULL to specify
  468.    the default heap that grows via sbrk.
  469.  
  470.    Note that for freshly created regions, we must call mmcheck prior to any
  471.    mallocs in the region.  Otherwise, any region which was allocated prior to
  472.    installing the checking hooks, which is later reallocated or freed, will
  473.    fail the checks!  The mmcheck function only allows initial hooks to be
  474.    installed before the first mmalloc.  However, anytime after we have called
  475.    mmcheck the first time to install the checking hooks, we can call it again
  476.    to update the function pointer to the memory corruption handler.
  477.  
  478.    Returns zero on failure, non-zero on success. */
  479.  
  480. void
  481. init_malloc (md)
  482.      PTR md;
  483. {
  484.   if (!mmcheck (md, malloc_botch))
  485.     {
  486.       warning ("internal error: failed to install memory consistency checks");
  487.     }
  488.  
  489.   (void) mmtrace ();
  490. }
  491.  
  492. #endif /* Have mmalloc and want corruption checking  */
  493.  
  494. /* Called when a memory allocation fails, with the number of bytes of
  495.    memory requested in SIZE. */
  496.  
  497. NORETURN void
  498. nomem (size)
  499.      long size;
  500. {
  501.   if (size > 0)
  502.     {
  503.       fatal ("virtual memory exhausted: can't allocate %ld bytes.", size);
  504.     }
  505.   else
  506.     {
  507.       fatal ("virtual memory exhausted.");
  508.     }
  509. }
  510.  
  511. /* Like mmalloc but get error if no storage available, and protect against
  512.    the caller wanting to allocate zero bytes.  Whether to return NULL for
  513.    a zero byte request, or translate the request into a request for one
  514.    byte of zero'd storage, is a religious issue. */
  515.  
  516. PTR
  517. xmmalloc (md, size)
  518.      PTR md;
  519.      long size;
  520. {
  521.   register PTR val;
  522.  
  523.   if (size == 0)
  524.     {
  525.       val = NULL;
  526.     }
  527.   else if ((val = mmalloc (md, size)) == NULL)
  528.     {
  529.       nomem (size);
  530.     }
  531.   return (val);
  532. }
  533.  
  534. /* Like mrealloc but get error if no storage available.  */
  535.  
  536. PTR
  537. xmrealloc (md, ptr, size)
  538.      PTR md;
  539.      PTR ptr;
  540.      long size;
  541. {
  542.   register PTR val;
  543.  
  544.   if (ptr != NULL)
  545.     {
  546.       val = mrealloc (md, ptr, size);
  547.     }
  548.   else
  549.     {
  550.       val = mmalloc (md, size);
  551.     }
  552.   if (val == NULL)
  553.     {
  554.       nomem (size);
  555.     }
  556.   return (val);
  557. }
  558.  
  559. /* Like malloc but get error if no storage available, and protect against
  560.    the caller wanting to allocate zero bytes.  */
  561.  
  562. PTR
  563. xmalloc (size)
  564.      long size;
  565. {
  566.   return (xmmalloc ((void *) NULL, size));
  567. }
  568.  
  569. /* Like mrealloc but get error if no storage available.  */
  570.  
  571. PTR
  572. xrealloc (ptr, size)
  573.      PTR ptr;
  574.      long size;
  575. {
  576.   return (xmrealloc ((void *) NULL, ptr, size));
  577. }
  578.  
  579.  
  580. /* My replacement for the read system call.
  581.    Used like `read' but keeps going if `read' returns too soon.  */
  582.  
  583. int
  584. myread (desc, addr, len)
  585.      int desc;
  586.      char *addr;
  587.      int len;
  588. {
  589.   register int val;
  590.   int orglen = len;
  591.  
  592.   while (len > 0)
  593.     {
  594.       val = read (desc, addr, len);
  595.       if (val < 0)
  596.     return val;
  597.       if (val == 0)
  598.     return orglen - len;
  599.       len -= val;
  600.       addr += val;
  601.     }
  602.   return orglen;
  603. }
  604.  
  605. /* Make a copy of the string at PTR with SIZE characters
  606.    (and add a null character at the end in the copy).
  607.    Uses malloc to get the space.  Returns the address of the copy.  */
  608.  
  609. char *
  610. savestring (ptr, size)
  611.      const char *ptr;
  612.      int size;
  613. {
  614.   register char *p = (char *) xmalloc (size + 1);
  615.   bcopy (ptr, p, size);
  616.   p[size] = 0;
  617.   return p;
  618. }
  619.  
  620. char *
  621. msavestring (md, ptr, size)
  622.      void *md;
  623.      const char *ptr;
  624.      int size;
  625. {
  626.   register char *p = (char *) xmmalloc (md, size + 1);
  627.   bcopy (ptr, p, size);
  628.   p[size] = 0;
  629.   return p;
  630. }
  631.  
  632. /* The "const" is so it compiles under DGUX (which prototypes strsave
  633.    in <string.h>.  FIXME: This should be named "xstrsave", shouldn't it?
  634.    Doesn't real strsave return NULL if out of memory?  */
  635. char *
  636. strsave (ptr)
  637.      const char *ptr;
  638. {
  639.   return savestring (ptr, strlen (ptr));
  640. }
  641.  
  642. char *
  643. mstrsave (md, ptr)
  644.      void *md;
  645.      const char *ptr;
  646. {
  647.   return (msavestring (md, ptr, strlen (ptr)));
  648. }
  649.  
  650. void
  651. print_spaces (n, file)
  652.      register int n;
  653.      register FILE *file;
  654. {
  655.   while (n-- > 0)
  656.     fputc (' ', file);
  657. }
  658.  
  659. /* Ask user a y-or-n question and return 1 iff answer is yes.
  660.    Takes three args which are given to printf to print the question.
  661.    The first, a control string, should end in "? ".
  662.    It should not say how to answer, because we do that.  */
  663.  
  664. /* VARARGS */
  665. int
  666. query (va_alist)
  667.      va_dcl
  668. {
  669.   va_list args;
  670.   char *ctlstr;
  671.   register int answer;
  672.   register int ans2;
  673.  
  674.   /* Automatically answer "yes" if input is not from a terminal.  */
  675.   if (!input_from_terminal_p ())
  676.     return 1;
  677.  
  678.   while (1)
  679.     {
  680.       va_start (args);
  681.       ctlstr = va_arg (args, char *);
  682.       vfprintf (stdout, ctlstr, args);
  683.       va_end (args);
  684.       printf ("(y or n) ");
  685.       fflush (stdout);
  686.       answer = fgetc (stdin);
  687.       clearerr (stdin);        /* in case of C-d */
  688.       if (answer == EOF)    /* C-d */
  689.         return 1;
  690.       if (answer != '\n')    /* Eat rest of input line, to EOF or newline */
  691.     do 
  692.       {
  693.         ans2 = fgetc (stdin);
  694.         clearerr (stdin);
  695.       }
  696.         while (ans2 != EOF && ans2 != '\n');
  697.       if (answer >= 'a')
  698.     answer -= 040;
  699.       if (answer == 'Y')
  700.     return 1;
  701.       if (answer == 'N')
  702.     return 0;
  703.       printf ("Please answer y or n.\n");
  704.     }
  705. }
  706.  
  707.  
  708. /* Parse a C escape sequence.  STRING_PTR points to a variable
  709.    containing a pointer to the string to parse.  That pointer
  710.    should point to the character after the \.  That pointer
  711.    is updated past the characters we use.  The value of the
  712.    escape sequence is returned.
  713.  
  714.    A negative value means the sequence \ newline was seen,
  715.    which is supposed to be equivalent to nothing at all.
  716.  
  717.    If \ is followed by a null character, we return a negative
  718.    value and leave the string pointer pointing at the null character.
  719.  
  720.    If \ is followed by 000, we return 0 and leave the string pointer
  721.    after the zeros.  A value of 0 does not mean end of string.  */
  722.  
  723. int
  724. parse_escape (string_ptr)
  725.      char **string_ptr;
  726. {
  727.   register int c = *(*string_ptr)++;
  728.   switch (c)
  729.     {
  730.     case 'a':
  731.       return 007;        /* Bell (alert) char */
  732.     case 'b':
  733.       return '\b';
  734.     case 'e':            /* Escape character */
  735.       return 033;
  736.     case 'f':
  737.       return '\f';
  738.     case 'n':
  739.       return '\n';
  740.     case 'r':
  741.       return '\r';
  742.     case 't':
  743.       return '\t';
  744.     case 'v':
  745.       return '\v';
  746.     case '\n':
  747.       return -2;
  748.     case 0:
  749.       (*string_ptr)--;
  750.       return 0;
  751.     case '^':
  752.       c = *(*string_ptr)++;
  753.       if (c == '\\')
  754.     c = parse_escape (string_ptr);
  755.       if (c == '?')
  756.     return 0177;
  757.       return (c & 0200) | (c & 037);
  758.       
  759.     case '0':
  760.     case '1':
  761.     case '2':
  762.     case '3':
  763.     case '4':
  764.     case '5':
  765.     case '6':
  766.     case '7':
  767.       {
  768.     register int i = c - '0';
  769.     register int count = 0;
  770.     while (++count < 3)
  771.       {
  772.         if ((c = *(*string_ptr)++) >= '0' && c <= '7')
  773.           {
  774.         i *= 8;
  775.         i += c - '0';
  776.           }
  777.         else
  778.           {
  779.         (*string_ptr)--;
  780.         break;
  781.           }
  782.       }
  783.     return i;
  784.       }
  785.     default:
  786.       return c;
  787.     }
  788. }
  789.  
  790. /* Print the character C on STREAM as part of the contents
  791.    of a literal string whose delimiter is QUOTER.  */
  792.  
  793. void
  794. printchar (c, stream, quoter)
  795.      register int c;
  796.      FILE *stream;
  797.      int quoter;
  798. {
  799.  
  800.   if (c < 040 || (sevenbit_strings && c >= 0177)) {
  801.     switch (c)
  802.       {
  803.       case '\n':
  804.     fputs_filtered ("\\n", stream);
  805.     break;
  806.       case '\b':
  807.     fputs_filtered ("\\b", stream);
  808.     break;
  809.       case '\t':
  810.     fputs_filtered ("\\t", stream);
  811.     break;
  812.       case '\f':
  813.     fputs_filtered ("\\f", stream);
  814.     break;
  815.       case '\r':
  816.     fputs_filtered ("\\r", stream);
  817.     break;
  818.       case '\033':
  819.     fputs_filtered ("\\e", stream);
  820.     break;
  821.       case '\007':
  822.     fputs_filtered ("\\a", stream);
  823.     break;
  824.       default:
  825.     fprintf_filtered (stream, "\\%.3o", (unsigned int) c);
  826.     break;
  827.       }
  828.   } else {
  829.     if (c == '\\' || c == quoter)
  830.       fputs_filtered ("\\", stream);
  831.     fprintf_filtered (stream, "%c", c);
  832.   }
  833. }
  834.  
  835. /* Number of lines per page or UINT_MAX if paging is disabled.  */
  836. static unsigned int lines_per_page;
  837. /* Number of chars per line or UNIT_MAX is line folding is disabled.  */
  838. static unsigned int chars_per_line;
  839. /* Current count of lines printed on this page, chars on this line.  */
  840. static unsigned int lines_printed, chars_printed;
  841.  
  842. /* Buffer and start column of buffered text, for doing smarter word-
  843.    wrapping.  When someone calls wrap_here(), we start buffering output
  844.    that comes through fputs_filtered().  If we see a newline, we just
  845.    spit it out and forget about the wrap_here().  If we see another
  846.    wrap_here(), we spit it out and remember the newer one.  If we see
  847.    the end of the line, we spit out a newline, the indent, and then
  848.    the buffered output.
  849.  
  850.    wrap_column is the column number on the screen where wrap_buffer begins.
  851.      When wrap_column is zero, wrapping is not in effect.
  852.    wrap_buffer is malloc'd with chars_per_line+2 bytes. 
  853.      When wrap_buffer[0] is null, the buffer is empty.
  854.    wrap_pointer points into it at the next character to fill.
  855.    wrap_indent is the string that should be used as indentation if the
  856.      wrap occurs.  */
  857.  
  858. static char *wrap_buffer, *wrap_pointer, *wrap_indent;
  859. static int wrap_column;
  860.  
  861. /* ARGSUSED */
  862. static void 
  863. set_width_command (args, from_tty, c)
  864.      char *args;
  865.      int from_tty;
  866.      struct cmd_list_element *c;
  867. {
  868.   if (!wrap_buffer)
  869.     {
  870.       wrap_buffer = (char *) xmalloc (chars_per_line + 2);
  871.       wrap_buffer[0] = '\0';
  872.     }
  873.   else
  874.     wrap_buffer = (char *) xrealloc (wrap_buffer, chars_per_line + 2);
  875.   wrap_pointer = wrap_buffer;    /* Start it at the beginning */
  876. }
  877.  
  878. static void
  879. prompt_for_continue ()
  880. {
  881.   char *ignore;
  882.  
  883.   immediate_quit++;
  884.   ignore = gdb_readline ("---Type <return> to continue---");
  885.   if (ignore)
  886.     free (ignore);
  887.   chars_printed = lines_printed = 0;
  888.   immediate_quit--;
  889.   dont_repeat ();        /* Forget prev cmd -- CR won't repeat it. */
  890. }
  891.  
  892. /* Reinitialize filter; ie. tell it to reset to original values.  */
  893.  
  894. void
  895. reinitialize_more_filter ()
  896. {
  897.   lines_printed = 0;
  898.   chars_printed = 0;
  899. }
  900.  
  901. /* Indicate that if the next sequence of characters overflows the line,
  902.    a newline should be inserted here rather than when it hits the end. 
  903.    If INDENT is nonzero, it is a string to be printed to indent the
  904.    wrapped part on the next line.  INDENT must remain accessible until
  905.    the next call to wrap_here() or until a newline is printed through
  906.    fputs_filtered().
  907.  
  908.    If the line is already overfull, we immediately print a newline and
  909.    the indentation, and disable further wrapping.
  910.  
  911.    If we don't know the width of lines, but we know the page height,
  912.    we must not wrap words, but should still keep track of newlines
  913.    that were explicitly printed.
  914.  
  915.    INDENT should not contain tabs, as that
  916.    will mess up the char count on the next line.  FIXME.  */
  917.  
  918. void
  919. wrap_here(indent)
  920.   char *indent;
  921. {
  922.   if (wrap_buffer[0])
  923.     {
  924.       *wrap_pointer = '\0';
  925.       fputs (wrap_buffer, stdout);
  926.     }
  927.   wrap_pointer = wrap_buffer;
  928.   wrap_buffer[0] = '\0';
  929.   if (chars_per_line == UINT_MAX)        /* No line overflow checking */
  930.     {
  931.       wrap_column = 0;
  932.     }
  933.   else if (chars_printed >= chars_per_line)
  934.     {
  935.       puts_filtered ("\n");
  936.       puts_filtered (indent);
  937.       wrap_column = 0;
  938.     }
  939.   else
  940.     {
  941.       wrap_column = chars_printed;
  942.       wrap_indent = indent;
  943.     }
  944. }
  945.  
  946. /* Like fputs but pause after every screenful, and can wrap at points
  947.    other than the final character of a line.
  948.    Unlike fputs, fputs_filtered does not return a value.
  949.    It is OK for LINEBUFFER to be NULL, in which case just don't print
  950.    anything.
  951.  
  952.    Note that a longjmp to top level may occur in this routine
  953.    (since prompt_for_continue may do so) so this routine should not be
  954.    called when cleanups are not in place.  */
  955.  
  956. void
  957. fputs_filtered (linebuffer, stream)
  958.      const char *linebuffer;
  959.      FILE *stream;
  960. {
  961.   const char *lineptr;
  962.  
  963.   if (linebuffer == 0)
  964.     return;
  965.   
  966.   /* Don't do any filtering if it is disabled.  */
  967.   if (stream != stdout
  968.    || (lines_per_page == UINT_MAX && chars_per_line == UINT_MAX))
  969.     {
  970.       fputs (linebuffer, stream);
  971.       return;
  972.     }
  973.  
  974.   /* Go through and output each character.  Show line extension
  975.      when this is necessary; prompt user for new page when this is
  976.      necessary.  */
  977.   
  978.   lineptr = linebuffer;
  979.   while (*lineptr)
  980.     {
  981.       /* Possible new page.  */
  982.       if (lines_printed >= lines_per_page - 1)
  983.     prompt_for_continue ();
  984.  
  985.       while (*lineptr && *lineptr != '\n')
  986.     {
  987.       /* Print a single line.  */
  988.       if (*lineptr == '\t')
  989.         {
  990.           if (wrap_column)
  991.         *wrap_pointer++ = '\t';
  992.           else
  993.         putc ('\t', stream);
  994.           /* Shifting right by 3 produces the number of tab stops
  995.              we have already passed, and then adding one and
  996.          shifting left 3 advances to the next tab stop.  */
  997.           chars_printed = ((chars_printed >> 3) + 1) << 3;
  998.           lineptr++;
  999.         }
  1000.       else
  1001.         {
  1002.           if (wrap_column)
  1003.         *wrap_pointer++ = *lineptr;
  1004.           else
  1005.             putc (*lineptr, stream);
  1006.           chars_printed++;
  1007.           lineptr++;
  1008.         }
  1009.       
  1010.       if (chars_printed >= chars_per_line)
  1011.         {
  1012.           unsigned int save_chars = chars_printed;
  1013.  
  1014.           chars_printed = 0;
  1015.           lines_printed++;
  1016.           /* If we aren't actually wrapping, don't output newline --
  1017.          if chars_per_line is right, we probably just overflowed
  1018.          anyway; if it's wrong, let us keep going.  */
  1019.           if (wrap_column)
  1020.         putc ('\n', stream);
  1021.  
  1022.           /* Possible new page.  */
  1023.           if (lines_printed >= lines_per_page - 1)
  1024.         prompt_for_continue ();
  1025.  
  1026.           /* Now output indentation and wrapped string */
  1027.           if (wrap_column)
  1028.         {
  1029.           if (wrap_indent)
  1030.             fputs (wrap_indent, stream);
  1031.           *wrap_pointer = '\0';        /* Null-terminate saved stuff */
  1032.           fputs (wrap_buffer, stream);    /* and eject it */
  1033.           /* FIXME, this strlen is what prevents wrap_indent from
  1034.              containing tabs.  However, if we recurse to print it
  1035.              and count its chars, we risk trouble if wrap_indent is
  1036.              longer than (the user settable) chars_per_line. 
  1037.              Note also that this can set chars_printed > chars_per_line
  1038.              if we are printing a long string.  */
  1039.           chars_printed = strlen (wrap_indent)
  1040.                 + (save_chars - wrap_column);
  1041.           wrap_pointer = wrap_buffer;    /* Reset buffer */
  1042.           wrap_buffer[0] = '\0';
  1043.           wrap_column = 0;        /* And disable fancy wrap */
  1044.          }
  1045.         }
  1046.     }
  1047.  
  1048.       if (*lineptr == '\n')
  1049.     {
  1050.       chars_printed = 0;
  1051.       wrap_here ((char *)0);  /* Spit out chars, cancel further wraps */
  1052.       lines_printed++;
  1053.       putc ('\n', stream);
  1054.       lineptr++;
  1055.     }
  1056.     }
  1057. }
  1058.  
  1059.  
  1060. /* fputs_demangled is a variant of fputs_filtered that
  1061.    demangles g++ names.*/
  1062.  
  1063. void
  1064. fputs_demangled (linebuffer, stream, arg_mode)
  1065.      char *linebuffer;
  1066.      FILE *stream;
  1067.      int arg_mode;
  1068. {
  1069. #define SYMBOL_MAX 1024
  1070.  
  1071. #define SYMBOL_CHAR(c) (isascii(c) \
  1072.   && (isalnum(c) || (c) == '_' || (c) == CPLUS_MARKER))
  1073.  
  1074.   char buf[SYMBOL_MAX+1];
  1075. # define SLOP 5        /* How much room to leave in buf */
  1076.   char *p;
  1077.  
  1078.   if (linebuffer == NULL)
  1079.     return;
  1080.  
  1081.   /* If user wants to see raw output, no problem.  */
  1082.   if (!demangle) {
  1083.     fputs_filtered (linebuffer, stream);
  1084.     return;
  1085.   }
  1086.  
  1087.   p = linebuffer;
  1088.  
  1089.   while ( *p != (char) 0 ) {
  1090.     int i = 0;
  1091.  
  1092.     /* collect non-interesting characters into buf */
  1093.     while ( *p != (char) 0 && !SYMBOL_CHAR(*p) && i < (int)sizeof(buf)-SLOP ) {
  1094.       buf[i++] = *p;
  1095.       p++;
  1096.     }
  1097.     if (i > 0) {
  1098.       /* output the non-interesting characters without demangling */
  1099.       buf[i] = (char) 0;
  1100.       fputs_filtered(buf, stream);
  1101.       i = 0;  /* reset buf */
  1102.     }
  1103.  
  1104.     /* and now the interesting characters */
  1105.     while (i < SYMBOL_MAX
  1106.      && *p != (char) 0
  1107.      && SYMBOL_CHAR(*p)
  1108.      && i < (int)sizeof(buf) - SLOP) {
  1109.       buf[i++] = *p;
  1110.       p++;
  1111.     }
  1112.     buf[i] = (char) 0;
  1113.     if (i > 0) {
  1114.       char * result;
  1115.       
  1116.       if ( (result = cplus_demangle(buf, arg_mode)) != NULL ) {
  1117.     fputs_filtered(result, stream);
  1118.     free(result);
  1119.       }
  1120.       else {
  1121.     fputs_filtered(buf, stream);
  1122.       }
  1123.     }
  1124.   }
  1125. }
  1126.  
  1127. /* Print a variable number of ARGS using format FORMAT.  If this
  1128.    information is going to put the amount written (since the last call
  1129.    to INITIALIZE_MORE_FILTER or the last page break) over the page size,
  1130.    print out a pause message and do a gdb_readline to get the users
  1131.    permision to continue.
  1132.  
  1133.    Unlike fprintf, this function does not return a value.
  1134.  
  1135.    We implement three variants, vfprintf (takes a vararg list and stream),
  1136.    fprintf (takes a stream to write on), and printf (the usual).
  1137.  
  1138.    Note that this routine has a restriction that the length of the
  1139.    final output line must be less than 255 characters *or* it must be
  1140.    less than twice the size of the format string.  This is a very
  1141.    arbitrary restriction, but it is an internal restriction, so I'll
  1142.    put it in.  This means that the %s format specifier is almost
  1143.    useless; unless the caller can GUARANTEE that the string is short
  1144.    enough, fputs_filtered should be used instead.
  1145.  
  1146.    Note also that a longjmp to top level may occur in this routine
  1147.    (since prompt_for_continue may do so) so this routine should not be
  1148.    called when cleanups are not in place.  */
  1149.  
  1150. static void
  1151. vfprintf_filtered (stream, format, args)
  1152.      FILE *stream;
  1153.      char *format;
  1154.      va_list args;
  1155. {
  1156.   static char *linebuffer = (char *) 0;
  1157.   static int line_size;
  1158.   int format_length;
  1159.  
  1160.   format_length = strlen (format);
  1161.  
  1162.   /* Allocated linebuffer for the first time.  */
  1163.   if (!linebuffer)
  1164.     {
  1165.       linebuffer = (char *) xmalloc (255);
  1166.       line_size = 255;
  1167.     }
  1168.  
  1169.   /* Reallocate buffer to a larger size if this is necessary.  */
  1170.   if (format_length * 2 > line_size)
  1171.     {
  1172.       line_size = format_length * 2;
  1173.  
  1174.       /* You don't have to copy.  */
  1175.       free (linebuffer);
  1176.       linebuffer = (char *) xmalloc (line_size);
  1177.     }
  1178.  
  1179.  
  1180.   /* This won't blow up if the restrictions described above are
  1181.      followed.   */
  1182.   (void) vsprintf (linebuffer, format, args);
  1183.  
  1184.   fputs_filtered (linebuffer, stream);
  1185. }
  1186.  
  1187. /* VARARGS */
  1188. void
  1189. fprintf_filtered (va_alist)
  1190.      va_dcl
  1191. {
  1192.   FILE *stream;
  1193.   char *format;
  1194.   va_list args;
  1195.  
  1196.   va_start (args);
  1197.   stream = va_arg (args, FILE *);
  1198.   format = va_arg (args, char *);
  1199.  
  1200.   /* This won't blow up if the restrictions described above are
  1201.      followed.   */
  1202.   vfprintf_filtered (stream, format, args);
  1203.   va_end (args);
  1204. }
  1205.  
  1206. /* VARARGS */
  1207. void
  1208. printf_filtered (va_alist)
  1209.      va_dcl
  1210. {
  1211.   va_list args;
  1212.   char *format;
  1213.  
  1214.   va_start (args);
  1215.   format = va_arg (args, char *);
  1216.  
  1217.   vfprintf_filtered (stdout, format, args);
  1218.   va_end (args);
  1219. }
  1220.  
  1221. /* Easy */
  1222.  
  1223. void
  1224. puts_filtered (string)
  1225.      char *string;
  1226. {
  1227.   fputs_filtered (string, stdout);
  1228. }
  1229.  
  1230. /* Return a pointer to N spaces and a null.  The pointer is good
  1231.    until the next call to here.  */
  1232. char *
  1233. n_spaces (n)
  1234.      int n;
  1235. {
  1236.   register char *t;
  1237.   static char *spaces;
  1238.   static int max_spaces;
  1239.  
  1240.   if (n > max_spaces)
  1241.     {
  1242.       if (spaces)
  1243.     free (spaces);
  1244.       spaces = (char *) xmalloc (n+1);
  1245.       for (t = spaces+n; t != spaces;)
  1246.     *--t = ' ';
  1247.       spaces[n] = '\0';
  1248.       max_spaces = n;
  1249.     }
  1250.  
  1251.   return spaces + max_spaces - n;
  1252. }
  1253.  
  1254. /* Print N spaces.  */
  1255. void
  1256. print_spaces_filtered (n, stream)
  1257.      int n;
  1258.      FILE *stream;
  1259. {
  1260.   fputs_filtered (n_spaces (n), stream);
  1261. }
  1262.  
  1263. /* C++ demangler stuff.  */
  1264.  
  1265. /* Print NAME on STREAM, demangling if necessary.  */
  1266. void
  1267. fprint_symbol (stream, name)
  1268.      FILE *stream;
  1269.      char *name;
  1270. {
  1271.   char *demangled;
  1272.   if ((!demangle) || NULL == (demangled = cplus_demangle (name, 1)))
  1273.     fputs_filtered (name, stream);
  1274.   else
  1275.     {
  1276.       fputs_filtered (demangled, stream);
  1277.       free (demangled);
  1278.     }
  1279. }
  1280.  
  1281. void
  1282. _initialize_utils ()
  1283. {
  1284.   struct cmd_list_element *c;
  1285.  
  1286.   c = add_set_cmd ("width", class_support, var_uinteger, 
  1287.           (char *)&chars_per_line,
  1288.           "Set number of characters gdb thinks are in a line.",
  1289.           &setlist);
  1290.   add_show_from_set (c, &showlist);
  1291.   c->function.sfunc = set_width_command;
  1292.  
  1293.   add_show_from_set
  1294.     (add_set_cmd ("height", class_support,
  1295.           var_uinteger, (char *)&lines_per_page,
  1296.           "Set number of lines gdb thinks are in a page.", &setlist),
  1297.      &showlist);
  1298.   
  1299.   /* These defaults will be used if we are unable to get the correct
  1300.      values from termcap.  */
  1301.   lines_per_page = 24;
  1302.   chars_per_line = 80;
  1303. #ifdef sprite  
  1304.   isatty_stdout = ISATTY(stdout);
  1305. #endif  
  1306.   /* Initialize the screen height and width from termcap.  */
  1307.   {
  1308.     char *termtype = getenv ("TERM");
  1309.  
  1310.     /* Positive means success, nonpositive means failure.  */
  1311.     int status;
  1312.  
  1313.     /* 2048 is large enough for all known terminals, according to the
  1314.        GNU termcap manual.  */
  1315.     char term_buffer[2048];
  1316.  
  1317.     if (termtype)
  1318.       {
  1319.     status = tgetent (term_buffer, termtype);
  1320.     if (status > 0)
  1321.       {
  1322.         int val;
  1323.         
  1324.         val = tgetnum ("li");
  1325.         if (val >= 0)
  1326.           lines_per_page = val;
  1327.         else
  1328.           /* The number of lines per page is not mentioned
  1329.          in the terminal description.  This probably means
  1330.          that paging is not useful (e.g. emacs shell window),
  1331.          so disable paging.  */
  1332.           lines_per_page = UINT_MAX;
  1333.         
  1334.         val = tgetnum ("co");
  1335.         if (val >= 0)
  1336.           chars_per_line = val;
  1337.       }
  1338.       }
  1339.   }
  1340.  
  1341. #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
  1342.  
  1343.   /* If there is a better way to determine the window size, use it. */
  1344.   SIGWINCH_HANDLER ();
  1345. #endif
  1346.  
  1347.   /* If the output is not a terminal, don't paginate it.  */
  1348.   if (!ISATTY (stdout))
  1349.     lines_per_page = UINT_MAX;
  1350.  
  1351.   set_width_command ((char *)NULL, 0, c);
  1352.  
  1353.   add_show_from_set
  1354.     (add_set_cmd ("demangle", class_support, var_boolean, 
  1355.           (char *)&demangle,
  1356.         "Set demangling of encoded C++ names when displaying symbols.",
  1357.           &setprintlist),
  1358.      &showprintlist);
  1359.  
  1360.   add_show_from_set
  1361.     (add_set_cmd ("sevenbit-strings", class_support, var_boolean, 
  1362.           (char *)&sevenbit_strings,
  1363.    "Set printing of 8-bit characters in strings as \\nnn.",
  1364.           &setprintlist),
  1365.      &showprintlist);
  1366.  
  1367.   add_show_from_set
  1368.     (add_set_cmd ("asm-demangle", class_support, var_boolean, 
  1369.           (char *)&asm_demangle,
  1370.     "Set demangling of C++ names in disassembly listings.",
  1371.           &setprintlist),
  1372.      &showprintlist);
  1373. }
  1374.  
  1375. /* Machine specific function to handle SIGWINCH signal. */
  1376.  
  1377. #ifdef  SIGWINCH_HANDLER_BODY
  1378.         SIGWINCH_HANDLER_BODY
  1379. #endif
  1380.